use "Output Data\reneg_analysis.dta", clear

rename *, lower
drop if year < 2000

egen comp_agency = group(companyid agencyid)
egen contract_group = group(award_id_piid parent_award_id_piid awarding_agency_code awarding_sub_agency_code)
egen agency_year = group(agencyid year)
egen comp_year = group(companyid year)
egen agency_ind = group(agencyid naics)
egen agency_ind_year = group(agencyid naics year)
egen award_group = group(award_type_code)
egen pricing_group = group(type_of_contract_pricing_code)

gen log_res = log(restrictions+1)
replace log_res = 0 if missing(log_res) & year <= 2019


gen reneg_per = reneg_obligation/total_obligation
winsor2 reneg_per if reneg_per > 0 & !missing(reneg_per), replace cuts(1 99)
gen offers_miss = 0
replace offers_miss = 1 if missing(n_offers)
replace n_offers = 0 if missing(n_offers)
gen log_offers = ln(n_offers+1)
gen log_ob = ln(total_obligation + 1)
replace log_ob = 0 if missing(log_ob)

gen connected = 0
replace connected = 1 if connect_num > 0 & !missing(connect_num)


reghdfe reneg_per connected log_res log_ob log_offers offers_miss i.award_group i.pricing_group if year <= 2018 & !missing(connect_num), a(agency_ind_year comp_year comp_agency) vce(cluster comp_agency) 
estimates store m1

egen zlog_ob = std(log_ob) if _est_m1 == 1
egen zlog_offers = std(log_offers) if _est_m1 == 1

reghdfe reneg connected zlog_ob zlog_offers offers_miss i.award_group i.pricing_group if year <= 2018 & !missing(connect_num) & _est_m1 == 1, a(agency_ind_year comp_year comp_agency) vce(cluster comp_agency) 
estimates store m2

reghdfe reneg connected zlog_ob zlog_offers offers_miss i.award_group i.pricing_group if year <= 2018 & !missing(connect_num) & _est_m1 == 1 & fixed_price == 1, a(agency_ind_year comp_year comp_agency) vce(cluster comp_agency) 
estimates store m3

reghdfe reneg connected zlog_ob zlog_offers offers_miss i.award_group i.pricing_group if year <= 2018 & !missing(connect_num) & _est_m1 == 1 & cost_plus == 1, a(agency_ind_year comp_year comp_agency) vce(cluster comp_agency) 
estimates store m4

reghdfe reneg_per connected zlog_ob zlog_offers offers_miss i.award_group i.pricing_group if year <= 2018 & !missing(connect_num) & reneg == 1, a(agency_ind_year comp_year comp_agency) vce(cluster comp_agency)
estimates store m5

reghdfe reneg_per connected zlog_ob zlog_offers offers_miss i.award_group i.pricing_group if year <= 2018 & !missing(connect_num) & reneg == 1 & fixed_price == 1, a(agency_ind_year comp_year comp_agency) vce(cluster comp_agency)
estimates store m6

reghdfe reneg_per connected zlog_ob zlog_offers offers_miss i.award_group i.pricing_group if year <= 2018 & !missing(connect_num) & reneg == 1 & cost_plus == 1, a(agency_ind_year comp_year comp_agency) vce(cluster comp_agency)
estimates store m7


#delimit ;
esttab m2 m3 m4 m5 m6 m7 using output.csv, 
	starlevels(* 0.10 ** 0.05 *** 0.01)
	cells(b(star fmt(%9.6f)) t(par fmt(%9.2f)))
	ar2
	replace;
#delimit cr


sum reneg if _est_m2 == 1
sum reneg if _est_m3 == 1
sum reneg if _est_m4 == 1
sum reneg_per if _est_m5 == 1
sum reneg_per if _est_m6 == 1
sum reneg_per if _est_m7 == 1


replace connected = 0
replace connected = 1 if connect_num_dw > 0 & !missing(connect_num_dw)
drop zlog_ob zlog_offers


reghdfe reneg_per connected log_res log_ob log_offers offers_miss i.award_group i.pricing_group if year <= 2018 & !missing(connect_num), a(agency_ind_year comp_year comp_agency) vce(cluster comp_agency) 
estimates store m1

egen zlog_ob = std(log_ob) if _est_m1 == 1
egen zlog_offers = std(log_offers) if _est_m1 == 1

reghdfe reneg connected zlog_ob zlog_offers offers_miss i.award_group i.pricing_group if year <= 2018 & !missing(connect_num) & _est_m1 == 1, a(agency_ind_year comp_year comp_agency) vce(cluster comp_agency) 
estimates store m2

reghdfe reneg_per connected zlog_ob zlog_offers offers_miss i.award_group i.pricing_group if year <= 2018 & !missing(connect_num) & reneg == 1, a(agency_ind_year comp_year comp_agency) vce(cluster comp_agency)
estimates store m3


#delimit ;
esttab m2 m3 output.csv, append
	starlevels(* 0.10 ** 0.05 *** 0.01)
	cells(b(star fmt(%9.6f)) t(par fmt(%9.2f)))
	ar2;
#delimit cr

sum reneg if _est_m2 == 1
sum reneg_per if _est_m3 == 1